package com.yulongtian.oneProblemEveryDay.month202301;

/**
 * https://leetcode.cn/problems/minimum-number-of-operations-to-reinitialize-a-permutation/
 *
 * @author yulongTian
 * @create 2023-01-09 10:37
 */
public class Test08 {
    public static void main(String[] args) {

    }

    public static int reinitializePermutation(int n) {
        int[] arr = new int[n];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = i;
        }
        int ans = 0;


        //先执行一次操作
        process(arr);
        ans++;
        while (!check(arr)) {
            process(arr);
            ans++;
        }
        return ans;
    }

    //检查
    public static boolean check(int[] arr) {
        boolean flag = true;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != i) {
                flag = false;
                break;
            }
        }
        return flag;
    }

    //每次操作
    public static void process(int[] arr) {
        int[] clone = arr.clone();
        for (int i = 0; i < arr.length; i++) {
            if ((i & 1) == 0) {
                arr[i] = clone[i / 2];
            } else {
                arr[i] = clone[arr.length / 2 + (i - 1) / 2];
            }
        }
    }

}
